divide and conquer program in c

90

#include<stdio.h>

int arr[8]={1, 2, 3, 4, 5, 6, 7, 8};

int main()
{
    int i;
    merge_sort(arr, 0, 7);

    printf("Sorted array:");

    for(i = 0; i < 8; i++)
        printf("%d", arr[i]);

    return 0;
}

int merge_sort(int arr[],int low,int high)
{
    printf("\nmerge_sort initialization\n");

    int mid;

    if(low < high) 
    {
        mid = (low + high) / 2;

        // Divide and Conquer
        merge_sort(arr, low, mid); 
        printf("\n merge_sort first\n");

        merge_sort(arr, mid + 1, high); 
        printf("\n merge_sort second\n");

        // Combine
        merge(arr, low, mid, high); 
        printf("\nmerging\n");
    }

    return 0;
}

int merge(int arr[], int l, int m, int h)
{
    int arr1[10], arr2[10];
    int n1, n2, i, j, k;
    n1 = m - l + 1;
    n2 = h - m;

    for(i = 0; i < n1; i++)
        arr1[i] = arr[l + i];

    for(j = 0; j < n2; j++)
        arr2[j] = arr[m + j + 1];

    arr1[i] = 9999;
    arr2[j] = 9999;

    i = 0;
    j = 0;

    for(k = l; k <= h; k++)
    {
        if(arr1[i] <= arr2[j])
            arr[k] = arr1[i++];
        else
            arr[k] = arr2[j++];
    }

    return 0;
}

Comments

Submit
0 Comments

More Questions

boolean in crandom number c ModuleNotFoundError: No module named cv2
check dns server in linuxread files in c how to get user input in c
print boolean value in clatex font sizes see if two strings are equal in C
how to download file in powershellprintf with bool invoke-webrequest download file
write in file in cmongodb delete all documents how to genrate a random number in C
how to print boolean in csleep in c programming install zoom on ubuntu
c concatenate stringstake array as input in c install gitk mac
remove element from np arrayprint an array in c C static libraries (creating object files)
exponentials in cC++ initalize int16_t value script hack blox fruti
pointer operatorcheck command line input is a number in c what is covert channel
sqlserver insert with set identity-usr-bin-env: python-r: No such file or directory linear search program in c
Uri-beecrowd problem no - 1099 solution in Cc addition The fscanf and fprintf functions
function that reverses the content of an array of integers.how to search in a file in c Couldnt create temporary file to work with
WARNING: QA Issue: bgslibrary-dev rdepends on libopencv-imgproc, but it isnt a build dependency, missing opencv in DEPENDS or PACKAGECONFIG [build-deps]reading arrays from stdin c add a item to cart woocomerce with quantity
Using PostgreSQL array to store many-to-many relationship using sqlalchemylistas enlazadas- linked lists binary sorting
c how to include variables of other c filec strcat uri-beecrowd problem no - 1133 solution in C